import { administrator, ADMINISTRATOR } from '../../src/lib/Member'


export default function(app, io) {
  const { socket } = app.locals;
  const { userId, username } = socket.handshake.query;

  console.log('------------------- adminMiddleware -----------------------')
  /**
   * 如果这个人在管理组，则将这个人加入管理组。
   */
  if (administrator.has(userId)) {
    for (const [classId, classroom] of (administrator)) {
      socket.join(classId)
      socket
        .broadcast
        .to(classId)
        .emit('online', {
          member: { userId, username }
        } as any);
    }
  }

  if (!socket.broadcastMemberForAdmin) {
    socket.broadcastMemberForAdmin = function broadcastMemberForAdmin(data) {
      console.warn(`member:state =>`, data)
      for (const [classId, classroom] of (administrator)) {
        socket
          .broadcast
          .to(classId)
          .emit('online', data);
      }
    }
  }

  if (!socket.broadcastClassForAdmin) {
    socket.broadcastClassForAdmin = function broadcastClassForAdmin(data) {
      console.warn(`classroom:state =>`, data)

      for (const [classId, classroom] of (administrator)) {
        socket
          .broadcast
          .to(classId)
          .emit('classroom:state', data);
      }
    }
  }
}
